﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.Odbc;
using System.Runtime.Remoting.Messaging;

namespace saveeditedrecordscodefile
{
    class saveedited
    {
        OdbcConnection donconnection = new OdbcConnection();
        OdbcDataAdapter donadapter = new OdbcDataAdapter();
        DataTable dondatatable = new DataTable();
        string pth = Environment.CurrentDirectory;
        public void dbConnect()
        {
            donconnection.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}" +
                ";DBQ=" + pth + "\\Database\\BenguetCorpEmployeeDatabase.accdb" +
                ";UID=" +
                ";PWD=";
            donconnection.Open();
        }
        public void dbQuery(string donSql)
        {
            donadapter.SelectCommand = new OdbcCommand(donSql, donconnection);
            dondatatable.Reset();
            dondatatable.Clear();
            donadapter.Fill(dondatatable);
            donconnection.Close();
        }
        public void donCommit()
        {
            OdbcCommandBuilder donconnectionb = new OdbcCommandBuilder(donadapter);

            donadapter.Update(dondatatable);

        }
        public DataTable donResult
        {
            get { return dondatatable; }
        }


        //save profile asynchronously
        public void donUpdate(int ii, string empnum, string estat, string ename, string edept, string epos, string egender, string econtact,
            string epresadd, string epermadd, string ehyt, string eweyt, string emarital, string ebday, string eage, string etin,
            string esss, string ephil, string epag, string eeducattain, string efaname, string efaadd, string efanum,
            string emoname, string emoadd, string emonum, string espousename, string espouseoccu, string eboardexam, string ecsc,
            string edriver, string eprc, string etesda, string eothers, string findsurname)
        {
            dondatatable.Rows[ii]["fEmployeeNumber"] = empnum;
            dondatatable.Rows[ii]["fEmployeeStatus"] = estat;
            dondatatable.Rows[ii]["fEmployeeName"] = ename;

            dondatatable.Rows[ii]["fDepartment"] = edept;
            dondatatable.Rows[ii]["fPosition"] = epos;
            dondatatable.Rows[ii]["fGender"] = egender;
            dondatatable.Rows[ii]["fContact"] = econtact;
            dondatatable.Rows[ii]["fPresentAddress"] = epresadd;
            dondatatable.Rows[ii]["fPermanentAddress"] = epermadd;
            dondatatable.Rows[ii]["fHeight"] = ehyt;
            dondatatable.Rows[ii]["fWeight"] = eweyt;
            dondatatable.Rows[ii]["fMaritalStatus"] = emarital;
            dondatatable.Rows[ii]["fBirthdate"] = ebday;
            dondatatable.Rows[ii]["fAge"] = eage;
            dondatatable.Rows[ii]["fTIN"] = etin;
            dondatatable.Rows[ii]["fSSS"] = esss;
            dondatatable.Rows[ii]["fPHILHEALTH"] = ephil;
            dondatatable.Rows[ii]["fPagibig"] = epag;
            dondatatable.Rows[ii]["fEducAttain"] = eeducattain;
            dondatatable.Rows[ii]["fFatherName"] = efaname;
            dondatatable.Rows[ii]["fFatherAddress"] = efaadd;
            dondatatable.Rows[ii]["fFatherNumber"] = efanum;
            dondatatable.Rows[ii]["fMotherName"] = emoname;
            dondatatable.Rows[ii]["fMotherAddress"] = emoadd;
            dondatatable.Rows[ii]["fMotherNumber"] = emonum;
            dondatatable.Rows[ii]["fSpouseName"] = espousename;
            dondatatable.Rows[ii]["fSpouseOccupation"] = espouseoccu;
            dondatatable.Rows[ii]["fBoardExamRating"] = eboardexam;
            dondatatable.Rows[ii]["fCSCEligibility"] = ecsc;
            dondatatable.Rows[ii]["fDriversLicense"] = edriver;
            dondatatable.Rows[ii]["fPRCNumber"] = eprc;
            dondatatable.Rows[ii]["fTesdaNCTitle"] = etesda;
            dondatatable.Rows[ii]["fOthers"] = eothers;
            dondatatable.Rows[ii]["fSurname"] = findsurname;
            

        }
        delegate void saveprofile(int ii, string empnum, string estat, string ename, string edept, string epos, string egender, string econtact,
            string epresadd, string epermadd, string ehyt, string eweyt, string emarital, string ebday, string eage, string etin,
            string esss, string ephil, string epag, string eeducattain, string efaname, string efaadd, string efanum,
            string emoname, string emoadd, string emonum, string espousename, string espouseoccu, string eboardexam, string ecsc,
            string edriver, string eprc, string etesda, string eothers, string findsurname);
        public void asyncsaveprofile(int ii, string empnum, string estat, string ename, string edept, string epos, string egender, string econtact,
            string epresadd, string epermadd, string ehyt, string eweyt, string emarital, string ebday, string eage, string etin,
            string esss, string ephil, string epag, string eeducattain, string efaname, string efaadd, string efanum,
            string emoname, string emoadd, string emonum, string espousename, string espouseoccu, string eboardexam, string ecsc,
            string edriver, string eprc, string etesda, string eothers, string findsurname)
        {
            saveprofile sp = new saveprofile(donUpdate);
            AsyncCallback ac = new AsyncCallback(cbsaveprofile);
            IAsyncResult ar = sp.BeginInvoke(ii,empnum,estat,ename,edept,epos,egender,econtact,epresadd,epermadd,ehyt,eweyt,emarital,ebday,eage,etin,esss,ephil,
                epag,eeducattain,efaname,efaadd,efanum,emoname,emoadd,emonum,espousename,espouseoccu,eboardexam,ecsc,edriver,eprc,etesda,eothers,findsurname,
                ac, null);
        }
        void cbsaveprofile(IAsyncResult ar)
        {
            saveprofile sp = (saveprofile)((AsyncResult)ar).AsyncDelegate;
            sp.EndInvoke(ar);
            onsavecompleted(null, null);
        }
        public event EventHandler savecompleted;
        protected virtual void onsavecompleted(object sender, EventArgs e)
        {
            if (savecompleted != null) savecompleted(sender, e);
        }
        //end of saving


 
    }
}